package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.ve.EditorHolder;
import com.stark.ve.core.IEditorListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.RecordBean;
import flc.ast.databinding.ActivityPlayFormatBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.media.video.VideoFormat;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.TimeUtil;

public class PlayFormatActivity extends BaseAc<ActivityPlayFormatBinding> {

    public static String videoPath = "";
    private String selFormat = "";
    private long videoLength;
    private Handler mHandler;
    private final Runnable mTaskUpdateTime = new Runnable() {
        @Override
        public void run() {
            mDataBinding.tvPlayLeft.setText(TimeUtil.getMmss(mDataBinding.videoView.getCurrentPosition()));
            mHandler.postDelayed(this,500);
        }
    };

    @Override
    protected int onCreate() {
        return R.layout.activity_play_format;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPlayFormatBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivPlayStart.setOnClickListener(this);
        mDataBinding.tvPlayFormatComp.setOnClickListener(this);
        mDataBinding.tvFormatMov.setOnClickListener(this);
        mDataBinding.tvFormatAvi.setOnClickListener(this);
        mDataBinding.tvFormatMkv.setOnClickListener(this);
        mDataBinding.tvFormat3gp.setOnClickListener(this);
        mDataBinding.tvFormatMp4.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivPlayStart:
                if (mDataBinding.videoView.isPlaying()){
                    mDataBinding.ivPlayStart.setImageResource(R.drawable.icon_bf);
                    mDataBinding.videoView.pause();
                }else{
                    mDataBinding.ivPlayStart.setImageResource(R.drawable.icon_zt);
                    mDataBinding.videoView.start();
                    startTime();
                }
                break;
            case R.id.tvPlayFormatComp:
                setFormat(getFormat());
                break;
            case R.id.tvFormatMp4:
                clearSelection();
                mDataBinding.tvFormatMp4.setBackgroundResource(R.drawable.gs_1);
                selFormat = "mp4";
                break;
            case R.id.tvFormatMkv:
                clearSelection();
                mDataBinding.tvFormatMkv.setBackgroundResource(R.drawable.gs_1);
                selFormat = "mkv";
                break;
            case R.id.tvFormatAvi:
                clearSelection();
                mDataBinding.tvFormatAvi.setBackgroundResource(R.drawable.gs_1);
                selFormat = "avi";
                break;
            case R.id.tvFormat3gp:
                clearSelection();
                mDataBinding.tvFormat3gp.setBackgroundResource(R.drawable.gs_1);
                selFormat = "3gp";
                break;
            case R.id.tvFormatMov:
                clearSelection();
                mDataBinding.tvFormatMov.setBackgroundResource(R.drawable.gs_1);
                selFormat = "mov";
                break;
        }
    }


    @Override
    protected void initData() {
        videoLength= MediaUtil.getDuration(videoPath);
        setPlayer();
        saveRecord(videoPath);
    }

    //设置视频格式
    private void setFormat(VideoFormat format) {
        EditorHolder.editor().videoFormatConvert(videoPath, format, new IEditorListener() {
            @Override
            public void onSuccess(String outFilePath) {
                dismissDialog();
                //保存到相册
                FileP2pUtil.copyPrivateVideoToPublic(mContext, outFilePath);
                ToastUtils.showShort(getString(R.string.save_to_album));
                saveRecord(outFilePath);
            }

            @Override
            public void onFailure(String errMsg) {
                dismissDialog();
                ToastUtils.showShort(R.string.conv_def);
            }

            @Override
            public void onProgress(int progress) {
                showDialog(getString(R.string.save_video_ing) + progress + getString(R.string.unit_percent));
            }
        });
    }

    //根据选择的格式获取格式后缀
    private VideoFormat getFormat() {
        VideoFormat format = VideoFormat.MP4;
        if (selFormat.equals("mp4")) {
            format = VideoFormat.MP4;
        } else if (selFormat.equals("avi")) {
            format = VideoFormat.AVI;
        } else if (selFormat.equals("mkv")) {
            format = VideoFormat.MKV;
        } else if (selFormat.equals("3gp")) {
            format = VideoFormat.THREE_GP;
        } else if (selFormat.equals("mov")) {
            format = VideoFormat.MOV;
        }
        return format;
    }

    //保存记录
    private void saveRecord(String videoPath) {
        List<RecordBean> listSave = new ArrayList<>();
        String time = TimeUtils.getNowString(new SimpleDateFormat("yyyy/MM/dd HH:mm"));
        listSave.add(new RecordBean(FileUtils.getFileName(videoPath),
                TimeUtil.getMmss(MediaUtil.getDuration(videoPath)), FileUtils.getSize(videoPath),
                time, videoPath));
        List<RecordBean> listSp = AppSpUtils.getPlayRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.savePlayRecordList(listSp);
        } else {
            AppSpUtils.savePlayRecordList(listSave);
        }
    }

    private void setPlayer() {
        mHandler=new Handler();
        mDataBinding.tvPlayLeft.setText("00:00");
        mDataBinding.tvPlayRight.setText(" / "+TimeUtil.getMmss(videoLength));
        mDataBinding.videoView.setVideoPath(videoPath);
        mDataBinding.videoView.seekTo(1);
        mDataBinding.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mDataBinding.tvPlayLeft.setText("00:00");
                mDataBinding.ivPlayStart.setImageResource(R.drawable.icon_bf);
                mDataBinding.videoView.seekTo(1);
                stopTime();
            }
        });
        //直接播放视频
        mDataBinding.videoView.seekTo(1);
        mDataBinding.ivPlayStart.setImageResource(R.drawable.icon_zt);
        mDataBinding.videoView.start();
        startTime();
    }

    private void stopTime() {
        mHandler.removeCallbacks(mTaskUpdateTime);
    }

    @Override
    protected void onStop() {
        super.onStop();
        stopTime();
    }

    private void startTime() {
        mHandler.post(mTaskUpdateTime);
    }

    private void clearSelection() {
        mDataBinding.tvFormatMp4.setBackgroundResource(R.drawable.gs_2);
        mDataBinding.tvFormatMkv.setBackgroundResource(R.drawable.gs_2);
        mDataBinding.tvFormat3gp.setBackgroundResource(R.drawable.gs_2);
        mDataBinding.tvFormatAvi.setBackgroundResource(R.drawable.gs_2);
        mDataBinding.tvFormatMov.setBackgroundResource(R.drawable.gs_2);
    }

}